import { h, ref } from 'vue';
import { cloneDeep } from 'lodash-es';
import { FormSchema } from '@/components/Form';
import { defRangeShortcuts } from '@/utils/dateUtil';
import { renderImage } from '@/utils';
import { renderOptionTag } from '@/utils';
import { useDictStore } from '@/store/modules/dict';

const dict = useDictStore();

export class State {
  public id = 0; // id
  public traceId = ''; // 记录 id
  public mid = 0; // 主记录 id
  public memberId = 0; // 分销至用户
  public productId = 0; // 商品 id
  public creditsLogId = 0; // 资金记录 id
  public amount = null; // 分销金额
  public proportion = 0; // 分销比例
  public createdAt = ''; // 分销时间
  public type = 0; // 分销类型

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

// 表单验证规则
export const rules = {
  traceId: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入记录 id',
  },
  mid: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入主记录 id',
  },
  memberId: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入分销至用户',
  },
  productId: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入商品 id',
  },
  creditsLogId: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入资金记录 id',
  },
};

// 表格搜索表单
export const schemas = ref<FormSchema[]>([
  // {
  //   field: 'id',
  //   component: 'NInputNumber',
  //   label: 'id',
  //   componentProps: {
  //     placeholder: '请输入id',
  //     onUpdateValue: (e: any) => {
  //       console.log(e);
  //     },
  //   },
  // },
  {
    field: 'traceId',
    component: 'NInput',
    label: '记录ID',
    componentProps: {
      placeholder: '请输入记录 id',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'adminMemberRoleId',
    component: 'NSelect',
    label: '角色',
    componentProps: {
      placeholder: '请选择角色',
      options: dict.getOption('xxb_yh_yhjs'),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'adminMemberMobile',
    component: 'NInput',
    label: '手机号码',
    componentProps: {
      placeholder: '请输入手机号码',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'adminMemberNickname',
    component: 'NInput',
    label: '微信昵称',
    componentProps: {
      placeholder: '请输入微信昵称',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'type',
    component: 'NSelect',
    label: '分销类型',
    componentProps: {
      placeholder: '请选择分销类型',
      options: dict.getOption('distributionDetailType'),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'createdAt',
    component: 'NDatePicker',
    label: '分销时间',
    componentProps: {
      type: 'datetimerange',
      clearable: true,
      shortcuts: defRangeShortcuts(),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

// 表格列
export const columns = [
  {
    title: '记录id',
    key: 'traceId',
    align: 'left',
    width: 280,
  },
  // {
  //   title: '主记录 id',
  //   key: 'mid',
  //   align: 'left',
  //   width: -1,
  // },
  // {
  //   title: '分销至用户',
  //   key: 'memberId',
  //   align: 'left',
  //   width: -1,
  // },
  // {
  //   title: '商品 id',
  //   key: 'productId',
  //   align: 'left',
  //   width: -1,
  // },
  // {
  //   title: '资金记录 id',
  //   key: 'creditsLogId',
  //   align: 'left',
  //   width: -1,
  // },
  {
    title: '分销金额',
    key: 'amount',
    align: 'left',
    width: -1,
  },
  {
    title: '分销比例',
    key: 'proportion',
    align: 'left',
    width: -1,
    render(row: any) {
      return row.proportion + '%';
    },
  },
  {
    title: '分销类型',
    key: 'type',
    align: 'left',
    width: 150,
    render(row: State) {
      return renderOptionTag('distributionDetailType', row.type);
    },
  },
  {
    title: '身份',
    key: 'adminMemberRoleId',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderOptionTag('xxb_yh_yhjs', row.adminMemberRoleId);
    },
  },
  {
    title: '手机号码',
    key: 'adminMemberMobile',
    align: 'left',
    width: -1,
  },
  {
    title: '微信昵称',
    key: 'adminMemberNickname',
    align: 'left',
    width: -1,
  },
  {
    title: '商品名称',
    key: 'productName',
    align: 'left',
    width: -1,
  },
  {
    title: '',
    key: 'productMainImage',
    align: 'left',
    width: -1,
    render(row: any) {
      return renderImage(row.productImages? row.productImages[0] : "");
    },
  },
  // {
  //   title: '商品配图',
  //   key: 'productImages',
  //   align: 'left',
  //   width: -1,
  // },
  // {
  //   title: '会员利润分配比例(%)',
  //   key: 'productVipProportion',
  //   align: 'left',
  //   width: -1,
  // },
  {
    title: '分销时间',
    key: 'createdAt',
    align: 'left',
    width: -1,
  },
];

// 加载字典数据选项
export function loadOptions() {
  dict.loadOptions(['xxb_yh_yhjs', 'distributionDetailType']);
}
